package com.iterator.controller.wechat.order;

import com.iterator.bean.web.pojo.BaseRespVo;
import com.iterator.bean.wechat.order.bo.CommentOrderBO;
import com.iterator.bean.wechat.order.bo.SubmitOrderBO;
import com.iterator.bean.wechat.order.pojo.WxOrderGoods;
import com.iterator.bean.wechat.order.vo.OrderListDataVO;
import com.iterator.bean.wechat.order.vo.OrderListVO;
import com.iterator.bean.wechat.order.vo.WxOrderDetailVO;
import com.iterator.bean.wechat.wy.address.AddressDetailVO;
import com.iterator.service.wechat.order.WxOrderService;
import com.iterator.service.wechat.wy.address.WxAddressService;
import org.apache.commons.collections.map.LinkedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : L
 * @ProjectName : project-2
 * @Date : 1/12/2021
 * @Description :
 */
@RestController
@RequestMapping("wx/order")
public class WxOrderController {
    @Autowired
    WxOrderService wxOrderService;
    @Autowired
    WxAddressService wxAddressService;

    @RequestMapping("submit")
    public BaseRespVo submit(@RequestBody SubmitOrderBO submitOrderBO) {
        if(submitOrderBO == null){
            return BaseRespVo.fail("参数非法");
        }
        AddressDetailVO addressDetailVO = wxAddressService.queryAddressDetailById(submitOrderBO.getAddressId());

        int addOrderedId = wxOrderService.createOrder(submitOrderBO,addressDetailVO);

        Map map = new LinkedMap();
        map.put("orderId",addOrderedId);

        return BaseRespVo.ok(map);
    }

    @RequestMapping("prepay")
    public BaseRespVo prepay(@RequestBody Map<String, Integer> map) {
        Integer orderId = map.get("orderId");
        Integer integer = wxOrderService.prepayOrderById(orderId);
        if (integer == 1) {
            return BaseRespVo.ok("已付款");
        }
        return BaseRespVo.fail("支付失败");
    }

    @RequestMapping("list")
    public BaseRespVo list(Integer showType, Integer page, Integer size) {
        List<OrderListDataVO> orderListDataVOS = wxOrderService.queryOrderListByShowType(showType, page, size);

        OrderListVO orderListVO = new OrderListVO();
        orderListVO.setData(orderListDataVOS);
        int size1 = orderListDataVOS.size();
        orderListVO.setCount(size1);

        if (size1 % size == 0) {
            orderListVO.setTotalPages(size1 / size);
        } else {
            orderListVO.setTotalPages(size1 / size + 1);
        }
        return BaseRespVo.ok(orderListVO);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId) {
        WxOrderDetailVO wxOrderDetailVO = wxOrderService.queryOrderDetailById(orderId);
        return BaseRespVo.ok(wxOrderDetailVO);
    }

    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody Map<String, Integer> map) {
        Integer orderId = map.get("orderId");
        Integer integer = wxOrderService.cancelOrderById(orderId);
        if (integer == 1) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.fail("取消订单失败");
        }
    }

    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody Map<String, Integer> map) {
        Integer orderId = map.get("orderId");
        Integer integer = wxOrderService.refundOrderById(orderId);
        if (integer == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail("退款申请失败");
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map<String, Integer> map) {
        Integer orderId = map.get("orderId");
        Integer integer = wxOrderService.deleteOrderById(orderId);
        if (integer == 1) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.fail("删除失败");
        }
    }

    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody Map<String, Integer> map) {
        Integer orderId = map.get("orderId");
        Integer integer = wxOrderService.confirmOrderById(orderId);
        if (integer == 1) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.fail("确认收货失败");
        }
    }

    @RequestMapping("goods")
    public BaseRespVo goods(Integer orderId, Integer goodsId) {
        List<WxOrderGoods> wxOrderGoods = wxOrderService.queryOrderGoods(orderId, goodsId);
        for (WxOrderGoods wxOrderGood : wxOrderGoods) {
            return BaseRespVo.ok(wxOrderGood);
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("comment")
    public BaseRespVo comment(@RequestBody CommentOrderBO commentOrderBO) {
        Integer i = wxOrderService.commentOrder(commentOrderBO);
        if (i == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail("评论失败");
    }

}
